package com.etc.servlet;

import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

@WebServlet("/search_init.ajax")
public class SearchInitServletAjax extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		ServletContext application = req.getServletContext();
		if (application.getAttribute("jlist") == null) {
			try {
				List<String> jlist = new ArrayList<>();
				Connection conn = JDBCUtil.getConn();
				
				PreparedStatement ps = conn.prepareStatement("select distinct job from emp");
				
				ResultSet rs = ps.executeQuery();
				
				while (rs.next()) {
					
					jlist.add(rs.getString(1));
				}

				conn.close();
							
				Gson g = new Gson();
				String json = g.toJson(jlist);
				
				resp.getWriter().append(json);
				return;
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	
}
